package com.sky.chapter3.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author : wushikai
 * <p>
 * date : 2022-07-05
 * 通过Thread类自带的中断api方法实现
 */
public class InterruptDemo1 {



    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("-----t1 线程被中断了，break，程序结束");
                    break;
                }
                System.out.println("-----hello");
            }
        }, "t1");
        t1.start();

        System.out.println("**************" + t1.isInterrupted());
        //暂停5毫秒
        try {
            TimeUnit.MILLISECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
        System.out.println("**************" + t1.isInterrupted());
    }
}
